package utils.files;

import com.google.common.base.Joiner;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import utils.VirtualFolder;

import java.io.IOException;
import java.nio.file.Files;
import java.util.Collections;

/**
 * Created by thormentor on 19-05-2014.
 */
public class File {
    private final VirtualFolder folder;
    private final String name;
    private final String extension;
    private final ObservableList<Object> objects = FXCollections.observableArrayList();

    public File(VirtualFolder folder, String name, String extension, Object... objects) {
        this.folder = folder;
        this.name = name;
        this.extension = extension;
        Collections.addAll(this.objects, objects);
    }

    public void add(Object o) {
        objects.add(o);
    }

    public void add(int index, Object o) {
        objects.add(index, o);
    }

    public void save() {
        try {
            Files.write(folder.get().createPath(name + "." + extension), getContent().getBytes());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public String getContent(){
        return Joiner.on("\n\n").join(objects);
    }
}
